package xyz.zhzby.cyzx.servicebase.exceptionhandler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import xyz.zhzby.cyzx.servicebase.domain.RestResponse;

/**
 * @author zby
 * @date 2021/12/07
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    //指定出现什么异常执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody //为了返回数据
    public RestResponse<String> error(Exception e) {
        String message = e.getMessage();
        if (message == null || message.length() > 20) {
            e.printStackTrace();
            message = "发生未知错误，请联系管理员";
        }
        return RestResponse.fail(message);
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public RestResponse<String> maxFile(MaxUploadSizeExceededException exception) {
        return RestResponse.fail("单个图片大小最大为2MB");
    }

}
